package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import model.CTPhieuXuat;
import model.KhachHang;
import model.MatHang;
import model.NhaCC;
import model.PhieuXuat;
import controller.CTPhieuXuatController;
import controller.NhanVienController;
import controller.PhieuXuatController;
import controller.KhachHangController;
import controller.MatHangController;
import controller.UserController;

public class HoaDonXuat extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JLabel label_1, label_2, label_3;
	private JTable table;
	// private JPanel panel_3;
	private int stt = 1;
	private double tong, thanhtien;
	private Vector<Vector> rowData = new Vector<>();
	private Vector<String> name = new Vector<>();;
	private java.util.List<CTPhieuXuat> listmh = new ArrayList<>();
	private NumberFormat n;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HoaDonXuat frame = new HoaDonXuat("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public HoaDonXuat(final String user) {
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 755, 590);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblQunLin = new JLabel("QUẢN LÝ CỬA HÀNG ĐIỆN MÁY");
		lblQunLin.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblQunLin.setForeground(SystemColor.textHighlight);
		lblQunLin.setBounds(210, 13, 318, 37);
		contentPane.add(lblQunLin);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(BorderFactory.createTitledBorder(""));
		panel_1.setBounds(12, 66, 155, 131);
		contentPane.add(panel_1);
		panel_1.setLayout(null);

		textField = new JTextField();
		textField.setBounds(11, 23, 114, 20);
		panel_1.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(11, 64, 114, 20);
		panel_1.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblSLng = new JLabel("S\u1ED1 l\u01B0\u1EE3ng");
		lblSLng.setBounds(12, 46, 53, 16);
		panel_1.add(lblSLng);

		JLabel lblNewLabel = new JLabel("Nh\u1EADp m\u00E3 s\u1EA3n ph\u1EA9m");
		lblNewLabel.setBounds(12, 3, 109, 16);
		lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
		panel_1.add(lblNewLabel);

		JButton btnXaDng = new JButton("Xóa dòng");
		btnXaDng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i=table.getSelectedRow();
				
			}
		});
		btnXaDng.setIcon(new ImageIcon(HoaDonXuat.class.getResource("/icon/Remove-item-icon.png")));
		btnXaDng.setBounds(596, 208, 126, 52);
		contentPane.add(btnXaDng);

		JButton btnNewButton = new JButton("Refresh");
		btnNewButton.setIcon(new ImageIcon(HoaDonXuat.class.getResource("/icon/Button-Refresh-icon.png")));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				HoaDonXuat h = new HoaDonXuat(user);
				h.setVisible(true);
			}
		});
		btnNewButton.setBounds(596, 280, 126, 52);
		contentPane.add(btnNewButton);

		JPanel panel = new JPanel();
		panel.setBounds(10, 200, 574, 209);
		contentPane.add(panel);

		name = new Vector<String>();
		name.addElement("STT");
		name.addElement("Tên mặt hàng");
		name.addElement("Số lượng");
		name.addElement("Đơn  giá");
		name.addElement("Thành tiền");
		getContentPane().add(panel, BorderLayout.CENTER);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 5, 574, 204);
		scrollPane.setViewportBorder(new LineBorder(SystemColor.activeCaption));
		panel.add(scrollPane);
		table = new JTable(rowData, name);
		table.setBounds(10, 80, 557, 209);
		scrollPane.add(table);
		scrollPane.setViewportView(table);

		panel.setLayout(null);
		JButton btnNewButton_1 = new JButton("");
		btnNewButton_1.setIcon(new ImageIcon(HoaDonXuat.class.getResource("/icon/Add-item-icon.png")));
		btnNewButton_1.setBounds(11, 92, 53, 27);
		panel_1.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					NumberFormat n = NumberFormat.getInstance();
					int id = Integer.parseInt(textField.getText());
					int sl = Integer.parseInt(textField_1.getText());
					//double tong = 0;
					MatHang v = MatHangController.getMatHang(id);
					CTPhieuXuat ct = new CTPhieuXuat();
					ct.setidMatHang(id);
					ct.setSoLuong(sl);
					ct.setMaHDPX(Integer.parseInt(textField_8.getText()));
					Vector ve = new Vector<>();
					ve.add(stt);
					ve.add(MatHangController.getMatHang(id).getTenMH());
					ve.add(sl);
					ve.add(n.format(v.getGia()));
					ve.add(n.format(v.getGia() * sl));
					rowData.add(ve);
					table.setModel(new DefaultTableModel(rowData, name));
					listmh.add(ct);
					stt++;
					for (int i = 0; i < listmh.size(); i++) {
						tong += MatHangController.getMatHang(
								listmh.get(i).getidMatHang()).getGia()
								* sl;
					}
					// n.format(tong);
					// Double.toString(tong);
					textField_4.setText(n.format(tong));
					textField.setText("");
					textField_1.setText("");
					// tinh so tien phai tra thanhtien
					if (textField_5.getText().equals("") == true) {
						JOptionPane.showMessageDialog(null,"Vui lòng nhập mã khách hàng hoặc chọn khách lẽ");
					} else if (textField_5.getText().equals("0") == true) {
						thanhtien = tong;
						textField_6.setText(n.format(thanhtien));

					} else {
						thanhtien = Math.round(tong-((tong* Double.parseDouble(textField_5.getText()) / 100)));
						textField_6.setText(n.format(thanhtien));
					}

				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"Nhập liệu bị lỗi vui lòng nhập lại");
					textField.setText("");
					textField_1.setText("");
				}
			}
		});

		// nut thanh toán

		JButton btnThanhTon = new JButton("Thanh toán");
		btnThanhTon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				CTPhieuXuatController c = new CTPhieuXuatController();
				int maHDPX = Integer.parseInt(textField_8.getText());
				for (int i = 0; i < listmh.size(); i++) {
					// Lưu xuống cơ sở dữ liệu
					int idmatHang = listmh.get(i).getidMatHang();
					MatHang m = MatHangController.getMatHang(idmatHang);
					int idncc = m.getIdNCC();
					String ten = m.getTenMH();
					double gia = m.getGia();
					double giaNhap = m.getGiaNhap();
					int slcl = m.getSoLuongMH() - listmh.get(i).getSoLuong();
					MatHangController.updateMatHang(new MatHang(idncc,
							idmatHang, ten, gia,giaNhap, slcl));
					// insert CTPX
					if (textField_7.equals("") == true) {
						int idNV = UserController.getUser(user).getIdnv();
						PhieuXuatController px = new PhieuXuatController();
						int idKH = Integer.parseInt(textField_7.getText());
						Date ngayXuat = new Date(Long.parseLong(label_1
								.getText()));
						px.insertPhieuXuat(new PhieuXuat(maHDPX, idNV, idKH,
								ngayXuat));
					} else {
						int idNV = UserController.getUser(user).getIdnv();
						PhieuXuatController px = new PhieuXuatController();
						// int idKH = Integer.parseInt(textField_7.getText());
						int idkh = 0;
						// Date ngayXuat = new
						// Date(Long.parseLong(label_1.getText()));
						java.util.Date d = new java.util.Date();
						Date ngayXuat = new Date(d.getYear(), d.getMonth(), d
								.getDate());
						px.insertPhieuXuat(new PhieuXuat(maHDPX, idNV, idkh,
								ngayXuat));
					}
					// insert phieu xuat
					int soLuong = listmh.get(i).getSoLuong();
					c.insertCTPhieuXuat(new CTPhieuXuat(maHDPX, idmatHang,
							soLuong));
				}
				JOptionPane.showMessageDialog(null,"Hóa đơn đã được lưu lại");
			}
		});
		btnThanhTon.setIcon(new ImageIcon(HoaDonXuat.class.getResource("/icon/payment-icon.png")));
		btnThanhTon.setBounds(448, 421, 142, 52);
		contentPane.add(btnThanhTon);

		JButton btnVTrangCh = new JButton("Thoát");
		btnVTrangCh.setIcon(new ImageIcon(HoaDonXuat.class.getResource("/icon/close-icon.png")));
		btnVTrangCh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// TrangChu c= new TrangChu(ten);
				// c.setVisible(true);
				dispose();
			}
		});
		btnVTrangCh.setBounds(596, 354, 126, 52);
		contentPane.add(btnVTrangCh);
		// / panel_2 chua thong tin ve khach hàng

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(BorderFactory.createTitledBorder(""));
		panel_2.setBounds(179, 66, 284, 131);
		contentPane.add(panel_2);
		panel_2.setLayout(null);

		JLabel lblKhchHng = new JLabel("Mã khách hàng");
		lblKhchHng.setBounds(12, 7, 86, 16);
		panel_2.add(lblKhchHng);

		textField_7 = new JTextField();
		textField_7.setBounds(110, 5, 99, 24);
		panel_2.add(textField_7);
		textField_7.setColumns(10);

		final JLabel label_2 = new JLabel("");
		label_2.setBounds(110, 72, 162, 16);
		panel_2.add(label_2);

		final JLabel label_3 = new JLabel("");
		label_3.setBounds(110, 98, 162, 16);
		panel_2.add(label_3);

		final JCheckBox chckbxKhchL = new JCheckBox("Khách lẽ");
		chckbxKhchL.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (chckbxKhchL.isSelected() == true) {
					textField_5.setText("0");
					textField_7.setText("");
					label_2.setText("");
					label_3.setText("");
				} else {
					textField_5.setText("");
					textField_7.setText("");
					label_2.setText("");
					label_3.setText("");
				}

			}
		});
		chckbxKhchL.setBounds(12, 38, 74, 24);
		panel_2.add(chckbxKhchL);
		JButton btnNewButton_2 = new JButton("OK");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int makh = Integer.parseInt(textField_7.getText());
				KhachHangController kh = new KhachHangController();
				// NhanVienController c = new NhanVienController();
				String tenkh = kh.getKHachHang(makh).getTenKH();
				label_2.setText(tenkh);
				// / doble ve so chuan
				NumberFormat n = NumberFormat.getInstance();
				String d = n.format(kh.getKHachHang(makh).getDiemTichLuy())
						+ " VNĐ";
				// /
				double dtl = kh.getKHachHang(makh).getDiemTichLuy();
				String sdtl = (Double.toString(dtl));
				label_3.setText(d);
				chckbxKhchL.setSelected(false);
				if (dtl < 10000000) {
					textField_5.setText("0");
				} else if (dtl >= 10000000 && dtl < 25000000) {
					textField_5.setText("5");
				} else if (dtl >= 25000000 && dtl < 40000000) {
					textField_5.setText("8");
				} else {
					textField_5.setText("10");
				}

			}
		});

		btnNewButton_2.setBounds(221, 5, 51, 24);
		panel_2.add(btnNewButton_2);

		JLabel lblTnKhchHng = new JLabel("Tên khách hàng");
		lblTnKhchHng.setBounds(12, 72, 90, 16);
		panel_2.add(lblTnKhchHng);

		JButton btnThmKh = new JButton("Thêm KH");
		btnThmKh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ThemKhachHang a = new ThemKhachHang();
				a.setVisible(true);
			}
		});
		btnThmKh.setBounds(110, 37, 162, 25);
		panel_2.add(btnThmKh);

		JLabel lblimTchLy = new JLabel("Điểm tích lũy");
		lblimTchLy.setBounds(12, 98, 86, 16);
		panel_2.add(lblimTchLy);

		// / panel_3 chua thong tin ve hoa don

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(BorderFactory.createTitledBorder(""));
		panel_3.setBounds(474, 67, 247, 130);
		contentPane.add(panel_3);
		panel_3.setLayout(null);

		JLabel lblNgyLpHa = new JLabel("Ngày lập");
		lblNgyLpHa.setBounds(12, 74, 98, 16);
		panel_3.add(lblNgyLpHa);

		JLabel lblNewLabel_1 = new JLabel("Nhân viên");
		lblNewLabel_1.setBounds(12, 46, 57, 16);
		panel_3.add(lblNewLabel_1);

		// set ten nhan vien
		JLabel label = new JLabel("");
		label.setBounds(115, 46, 120, 16);
		panel_3.add(label);
		int id = UserController.getUser(user).getIdnv();
		String ten = NhanVienController.getNhanVien(id).getTenNV();
		label.setText(ten);

		JLabel lblMHan = new JLabel("Mã hóa đơn");
		lblMHan.setBounds(12, 102, 98, 16);
		panel_3.add(lblMHan);

		JLabel label_1 = new JLabel("");
		label_1.setBounds(115, 74, 80, 16);
		panel_3.add(label_1);
		java.util.Date d = new java.util.Date();
		label_1.setText((d.getYear() + 1900) + "/" + (d.getMonth() + 1) + "/"
				+ d.getDate());

		JLabel lblCaHngin = new JLabel("CỬA HÀNG ĐIỆN MÁY NGUYỄN MỘC");
		lblCaHngin.setBounds(12, 12, 205, 29);
		panel_3.add(lblCaHngin);
		PhieuXuatController hd = new PhieuXuatController();
		int count = hd.getDSPhieuXuat().size() + 1;
		textField_8 = new JTextField();
		textField_8.setBounds(115, 102, 120, 20);
		panel_3.add(textField_8);
		textField_8.setColumns(10);
		textField_8.setText(count + "");

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(BorderFactory.createTitledBorder(""));
		panel_4.setBounds(12, 420, 424, 121);
		contentPane.add(panel_4);
		panel_4.setLayout(null);

		JLabel lblTngTin = new JLabel("Tổng tiền");
		lblTngTin.setBounds(12, 7, 52, 16);
		panel_4.add(lblTngTin);

		
		JLabel lblGimGi = new JLabel("Giảm giá");
		lblGimGi.setBounds(144, 7, 49, 16);
		panel_4.add(lblGimGi);

		textField_5 = new JTextField();
		textField_5.setBounds(144, 35, 91, 22);
		panel_4.add(textField_5);
		textField_5.setColumns(10);

		JLabel lblSTinThanh = new JLabel("Số tiền thanh toán");
		lblSTinThanh.setBounds(286, 7, 102, 16);
		panel_4.add(lblSTinThanh);

		textField_6 = new JTextField();
		textField_6.setBounds(286, 35, 114, 22);
		panel_4.add(textField_6);
		textField_6.setColumns(10);

		textField_4 = new JTextField();
		textField_4.setBounds(12, 35, 114, 22);
		panel_4.add(textField_4);
		textField_4.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Số tiền khách trả");
		lblNewLabel_2.setBounds(12, 65, 114, 16);
		panel_4.add(lblNewLabel_2);

		textField_2 = new JTextField();
		textField_2.setBounds(12, 91, 85, 22);
		panel_4.add(textField_2);
		textField_2.setColumns(10);

		JLabel lblThiLi = new JLabel("Thói lại");
		lblThiLi.setBounds(144, 65, 55, 16);
		panel_4.add(lblThiLi);

		textField_3 = new JTextField();
		textField_3.setBounds(144, 90, 114, 22);
		panel_4.add(textField_3);
		textField_3.setColumns(10);

		JLabel label_4 = new JLabel("%");
		label_4.setBounds(241, 38, 18, 16);
		panel_4.add(label_4);

		JButton button = new JButton("=");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (chckbxKhchL.isSelected()==true) {
						NumberFormat n = NumberFormat.getInstance();
						Double tiennhan = Double.parseDouble(textField_2.getText());
						Double thoi = tiennhan - thanhtien;
						textField_3.setText(n.format(thoi));
					}else{
						NumberFormat n = NumberFormat.getInstance();
						Double tiennhan = Double.parseDouble(textField_2.getText());
						Double thoi = tiennhan - thanhtien;
						textField_3.setText(n.format(thoi));
					}
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"Nhập liệu bị lỗi vui lòng nhập lại");

				}
			}
		});
		button.setBounds(91, 91, 41, 21);
		panel_4.add(button);

	}
}
